/**
 * 该类提供listview下拉刷新的功能。
 *
 * @author SunHang
 * @since 2011-11-22
 */

package com.zcl.customcontrols;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Scroller;
import android.widget.TextView;

import com.zcl.iweibo.R;
import com.zcl.utils.NqLog;
import com.zcl.utils.NqUtil;
import com.zcl.utils.Value;

public class PullDownView extends LinearLayout {

    public enum EPullStatus { // 此时listview处于什么状态
        PULL_NORMAL, // 普通状态
        PULL_DOWN, // 需要继续下拉才能刷新
        PULL_RELEASE, // 松开触摸屏刷新
        PULL_REFRESHING, // 下拉之后正在刷新
        MORE_REFRESHING // 点击更多正在刷新
    };

    private ListView mListView;
    private HeaderView mHeaderView; // 显示当前listview状态的控件，位于listview上边
    private ImageView mDivider; // header 和 listview之间的分割线

    private EPullStatus mPullStatus = EPullStatus.PULL_NORMAL; // 当前listview状态
    private int mFirstVisibleItem = -1; // listview中最上边的item的下标值

    private OnTouchDownUpListener mOnTouchDownUpListener;// 用于监听touchdown和touchup
    private OnRefreshListener mOnRefreshListener = null; // 当listview进行刷新时的回调
    private OnHeaderViewStateChangeListener mOnHeaderViewStateChangeListener = null; // 当headerview的状态改变时的回调
    private OnObtainMoreListener mOnObtainMoreListener = null; // 当点击“获取更多”时的回调
    private OnItemClickListener mPullDownViewOnItemClickListener = null; // 当点击items时的回调
    private OnPullDownListener mPullDownListener = null; // 监听开始下拉的listener
    private OnPullDownBackListener mOnPullDownBackListener = null; // 当headerview回弹到初始位置时触发

    private int mTouchSlop = 0;
    private boolean mMoved = false; // 记录用户是否只是拉动控件，而不是点击某一item
    private boolean mPullDownEnabled = true; // 该控件是否可以下拉刷新

    /**
     * 网络速度非常快时，“获取更多”从等待状态到普通状态转换速度特快，给用户感觉是“获取更多”状态没有发生变化。
     * 规定“获取更多”的等待状态最小时间间隔是1秒，用两个条件mTimesOff和mNotifie表示“获取更多”是否可以回到普通状态。
     * 
     * @author sunhang
     * 
     */
    /*
     * private class FooterStateTrigger{ final long mMinIntervalTime = 1000; // 最小等待时间为1秒 boolean
     * mTimesOff = true; // 1秒钟时间到 boolean mNotified = true; // 是否已经notify了 void set(){ //
     * “获取更多”处于等待状态时，调用该函数 mTimesOff = false; mNotified = false;
     * 
     * Timer timer = new Timer(); timer.schedule(new TimerTask(){
     * 
     * @Override public void run() { mTimesOff = true;
     * 
     * if (normalStateValid()){ resetFooter(); } }
     * 
     * }, mMinIntervalTime); }
     * 
     * 
     * // 是否需要返回到普通状态 boolean normalStateValid(){ if (mTimesOff && mNotified){ return true; }
     * 
     * return false; } } private FooterStateTrigger mFooterStateTrigger = new FooterStateTrigger();
     */
    private View mFooterView = null;
    // private int mFooterViewHeight = -1; // footerView的高度
    private Scroller mScroller;

    private String[] mStrHeaderStatusInfo;
    private String mLastUpdateTitle;
    private int mArrowIcon = R.drawable.goicon; // headerView中的箭头图标

    private final String TAG = "PullDownView";

    public PullDownView(Context context) {
        super(context);
        mScroller = new Scroller(context);
    }

    public PullDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    /**
     * 初始化
     */
    public void init() {
        initControls();

        // 初始化headerview中要显示的内容
        Context context = getContext();
        mStrHeaderStatusInfo = new String[] {
                context.getString(R.string.pull_to_refresh_pull_label), context.getString(R.string.pull_to_refresh_release_label),
                context.getString(R.string.pull_to_refresh_refreshing_label) };
        mLastUpdateTitle = context.getString(R.string.last_update);

        // 获取touchSlop
        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();

        mOnHeaderViewStateChangeListener = ON_HEADER_VIEW_STATE_CHANGE_LISTENER;
    }

    /**
     * 设置headerview中要显示的信息
     * 
     * @param strs
     *            :长度为3的字符串数组，分别为下拉状态提示语；回弹状态提示语；刷新状态提示语
     */
    public void setHeaderStatusInfo(String[] strs) {
        mStrHeaderStatusInfo = strs;
    }

    /**
     * 设置更新时间的标题栏
     * 
     * @param title
     */
    public void setLastUpdateTitle(String title) {
        mLastUpdateTitle = title;
    }

    /**
     * 初始化
     * 
     * @param layoutID
     *            headerview的布局，layoutID等于-1表示使用默认的布局
     */
    public void init(int layoutID) {
        initControls();

        if (layoutID == -1) {
            return;
        }

        mHeaderView.removeAllViews();

        // 添加headerView中的layout
        View view = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutID, null);
        mHeaderView.addView(view);

        mOnHeaderViewStateChangeListener = null;

        final ViewConfiguration configuration = ViewConfiguration.get(getContext());
        mTouchSlop = configuration.getScaledTouchSlop();
    }

    private void initControls() {
        // 获取到控件的引用
        mListView = (ListView) findViewById(R.id.pull_down_listview);
        mListView.setVerticalFadingEdgeEnabled(true);
        mHeaderView = (HeaderView) findViewById(R.id.pull_down_headerview);
        mDivider = (ImageView) findViewById(R.id.iv_divider);

        // 为listview设置监听函数
        mListView.setOnScrollListener(ON_LISTVIEW_SCROLL_LISTENER);
        mListView.setOnTouchListener(ON_LISTVIEW_TOUCH_LISTENER);
        mListView.setOnItemClickListener(ON_ITEM_CLICK_LISTENER);
    }

    public ListView getListView() {
        return mListView;
    }

    public HeaderView getHeaderView() {
        return mHeaderView;
    }

    /**
     * 设置该控件是否可以下拉刷新
     * 
     * @param pullDownEnabled
     */
    public void setPullDownEnabled(boolean pullDownEnabled) {
        this.mPullDownEnabled = pullDownEnabled;
    }

    /**
     * 监听开始下拉的listener
     * 
     * @param pullDownListener
     */
    public void setOnPullDownListener(OnPullDownListener pullDownListener) {
        mPullDownListener = pullDownListener;
    }

    /**
     * 当headerview回弹到初始位置时触发
     * 
     * @param onPullDownBackListener
     */
    public void setOnPullDownBackListener(OnPullDownBackListener onPullDownBackListener) {
        mOnPullDownBackListener = onPullDownBackListener;
    }

    /**
     * 设置下拉头的背景色
     * 
     * @param bkg
     *            :resource id
     */
    public void setHeaderViewBkg(int bkg) {
        mHeaderView.setBackgroundResource(bkg);
    }

    public void setHeaderViewBkg(Drawable d) {
        mHeaderView.setBackgroundDrawable(d);
    }

    /**
     * header和listview之间的分割线是否需要显示，默认是不显示
     * 
     * @param enabled
     */
    public void setDividerEnabled(boolean enabled) {
        if (enabled) {
            mDivider.setVisibility(View.VISIBLE);
        } else {
            mDivider.setVisibility(View.GONE);
        }
    }

    /**
     * 设置状态文字的颜色
     * 
     * @param color
     */
    public void setHeaderViewStatusTextColor(int color) {
        TextView textView = (TextView) mHeaderView.findViewById(R.id.status_info);
        textView.setTextColor(color);
    }

    /**
     * 设置更新时间的颜色
     * 
     * @param color
     */
    public void setHeaderViewUpdateTimeTextColor(int color) {
        TextView textView = (TextView) mHeaderView.findViewById(R.id.last_update);
        textView.setTextColor(color);
    }

    /**
     * 添加图片，表示状态栏中的箭头
     * 
     * @param res
     */
    public void setArrowImage(int res) {
        mArrowIcon = res;
    }

    /**
     * 更新时间控件的可见性的设置
     * 
     * @param visibility
     */
    public void setUpdateTimeVisibility(int visibility) {
        ((TextView) mHeaderView.findViewById(R.id.last_update)).setVisibility(visibility);
    }

    public void setFooterViewBkg(Drawable d) {
        if (null != mFooterView) {
            mFooterView.setBackgroundDrawable(d);
        }
    }

    /**
     * 刷新后调用该函数
     */
    public void notifyRefreshComplete() {
        setPullStatus(EPullStatus.PULL_NORMAL);

        onRefreshComplete();
    }

    /**
     * 是否要显示按钮“获取更多”
     * 
     * @param visibility
     */
    public void showFooterView(boolean show) {
        if (null != mFooterView) {
            if (show) {
                mFooterView.findViewById(R.id.textView_obtain_more).setVisibility(View.VISIBLE);
            } else {
                mFooterView.findViewById(R.id.textView_obtain_more).setVisibility(View.GONE);
            }
        }
    }

    /**
     * 是否处于EPullStatus.PULL_NORMAL状态
     * 
     * @return
     */
    public boolean isNormalState() {
        if (mPullStatus == EPullStatus.PULL_NORMAL) {
            return true;
        }

        return false;
    }

    /**
     * 刷新后的处理
     */
    private void onRefreshComplete() {
        resetHeader();

        /*
         * mFooterStateTrigger.mNotified = true; if (mFooterStateTrigger.normalStateValid()){
         * resetFooter(); }
         */

        resetFooter();
    }

    private void resetFooter() {
        if (null != mFooterView) {
            ProgressBar pb = (ProgressBar) mFooterView.findViewById(R.id.refresh_progress);
            pb.setVisibility(View.GONE);
            
            ((TextView)mFooterView.findViewById(R.id.textView_obtain_more)).setText(R.string.obtain_more);
        }
    }

    public void setOnTouchDownUpListener(OnTouchDownUpListener listener) {
        mOnTouchDownUpListener = listener;
    }

    /**
     * 设置刷新时的监听函数
     * 
     * @param onRefreshListener
     */
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    /**
     * 设置状态发生变化时的监听函数
     * 
     * @param onHeaderViewStateChangeListener
     */
    public void setOnHeaderViewStateChangeListener(OnHeaderViewStateChangeListener onHeaderViewStateChangeListener) {
        mOnHeaderViewStateChangeListener = onHeaderViewStateChangeListener;
    }

    /**
     * 设置获取更多信息时的监听函数
     * 
     * @param onObtainMoreListener
     */
    public void setOnObtainMoreListener(OnObtainMoreListener onObtainMoreListener) {
        this.mOnObtainMoreListener = onObtainMoreListener;
    }

    /**
     * 设置listview中的item被点击时的监听函数
     * 
     * @param pullDownViewOnItemClickListener
     */
    public void setPullDownViewOnItemClickListener(OnItemClickListener pullDownViewOnItemClickListener) {
        this.mPullDownViewOnItemClickListener = pullDownViewOnItemClickListener;
    }

    /**
     * 设置获取更多时的监听函数
     * 
     * @param layoutID
     */
    public void setFooterView(int layoutID) {
        View view = ((LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(layoutID, null);

        mFooterView = view;

        mListView.addFooterView(view);
    }

    /**
     * 设置获取更多时的监听函数
     * 
     * @param view
     */
    public void setFooterView(View view) {
        mFooterView = view;

        mListView.addFooterView(view);
    }

    /**
     * 设置footerview的文本，不一定只显示“获取更多”，也可以显示其它文字
     * 
     * @param text
     */
    public void setFooterViewText(String text) {
        if (null != mFooterView) {
            ((TextView) mFooterView.findViewById(R.id.textView_obtain_more)).setText(text);
        }
    }

    /**
     * 重新设置状态
     */
    private void resetHeader() {
        if (Value.printLog) {
            NqLog.d(TAG, "resetHeader");
        }
        // scrollTo(0, 0);

        int scrollY = getScrollY();
        mScroller.startScroll(0, scrollY, 0, -(scrollY), 800);
        invalidate();
    }

    /**
     * 准备刷新
     */
    private void preparePullRefresh() {
        if (Value.printLog) {
            NqLog.d(TAG, "prepareRefresh");
        }

        int height = mHeaderView.getHeight();
        if (height == 0) { // 外部控件可能在onCreate里边调用了该函数，所以高度为0
            height = NqUtil.convertDIP2PX(getContext(), 60);
        }

        int scrollY = getScrollY();
        // scrollTo(0, -height);
        mScroller.startScroll(0, scrollY, 0, -(scrollY + height), 800);
        invalidate();
    }

    /**
     * 设置成正在刷新状态
     */
    public void setRefreshState() {
        setPullStatus(EPullStatus.PULL_REFRESHING);
        preparePullRefresh();
    }

    /**
     * 设置最后一次更新的时间
     * 
     * @param lastUpdateTime
     *            最后一次更新的时间
     */
    public void setLastUpdateTime(String lastUpdateTime, Context context) {
        // 转换时间格式
        String newFormatTimeString = NqUtil.getFormatChineseTime(context, lastUpdateTime);
        ((TextView) mHeaderView.findViewById(R.id.last_update)).setText(mLastUpdateTitle + newFormatTimeString);
    }

    /**
     * 设置当前状态，当状态发生改变时，调用mOnHeaderViewStateChangeListener的回调
     * 
     * @param pullStatus
     *            当前状态
     */
    private void setPullStatus(EPullStatus pullStatus) {
        if (mPullStatus == pullStatus) {
            return;
        }

        /**
         * 当控件开始下拉时，调用该接口
         */
        if (mPullStatus == EPullStatus.PULL_NORMAL && pullStatus == EPullStatus.PULL_DOWN) {
            if (mPullDownListener != null)
                mPullDownListener.startPullDown();
        } else if (mPullStatus == EPullStatus.PULL_DOWN && pullStatus == EPullStatus.PULL_NORMAL) {
            if (mOnPullDownBackListener != null) {
                mOnPullDownBackListener.onPullDownBack();
            }
        }

        mPullStatus = pullStatus;
        if (mOnHeaderViewStateChangeListener != null) {
            mOnHeaderViewStateChangeListener.onStateChange(mHeaderView.getChildAt(0), mFooterView, mPullStatus);
        }
    }

    /**
     * 设置状态，获取更多
     */
    public void setObtainingMoreStatus() {
        setPullStatus(EPullStatus.MORE_REFRESHING);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        } else {
            super.computeScroll();
        }
    }

    /**
     * listview发生滚动时的接口调用
     */
    private final OnScrollListener ON_LISTVIEW_SCROLL_LISTENER = new OnScrollListener() {
        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            if (view != null) {
            }
        }
    };

    /**
     * 当触摸listview时的接口调用
     */
    private final OnTouchListener ON_LISTVIEW_TOUCH_LISTENER = new OnTouchListener() {
        private float mPreMotionY = -1;
        private float mFirstMotionY = -1; // 记录用户首次触摸屏幕时的位置

        private int mPointPosition = -1; // 被点击的item

        private final int FALSE = 0;
        private final int TRUE = 1;
        private final int OTHER = 2;

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (false == mPullDownEnabled
                    || EPullStatus.MORE_REFRESHING == mPullStatus || mListView.getCount() == 0
                    || EPullStatus.PULL_REFRESHING == mPullStatus) {
                return false;
            }

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                performActionDown(event);
                break;
            case MotionEvent.ACTION_MOVE: {
                switch (performActionMove(event)) {
                case FALSE:
                    return false;
                case TRUE:
                    return true;
                case OTHER:
                    break;
                }

            }
                break;
            case MotionEvent.ACTION_UP: // 当手势离开屏幕时
                performActionUp();

                break;
            }

            return false;
        }

        private void performActionDown(MotionEvent event) {
            if (mListView.getCount() <= 0) {
                return;
            }

            mPreMotionY = event.getRawY();
            mFirstMotionY = mPreMotionY;
            mMoved = false;
            mPointPosition = mListView.pointToPosition((int) event.getX(), (int) event.getY());
            if (mPointPosition != ListView.INVALID_POSITION && mOnTouchDownUpListener != null) {
                mOnTouchDownUpListener.onTouchDown(mPointPosition);
            }
            // // 设置当前状态
            // if (getScrollY() >= 0
            // && mFirstVisibleItem == 0
            // && (mListView.getChildAt(mFirstVisibleItem) != null &&
            // mListView.getChildAt(mFirstVisibleItem).getTop() == 0)) {
            // setPullStatus(EPullStatus.PULL_DOWN);
            // }
        }

        private void performActionUp() {
            if (mOnTouchDownUpListener != null && mPointPosition != ListView.INVALID_POSITION) {
                mOnTouchDownUpListener.OnTouchUp(mPointPosition);
            }
            mPreMotionY = -1;
            mPointPosition = -1;

            if (mPullStatus == EPullStatus.PULL_RELEASE) {
                if (Value.printLog) {
                    NqLog.d(TAG, "mPullStatus = EPullStatus.PULL_RELEASE");
                }

                setPullStatus(EPullStatus.PULL_REFRESHING);
                preparePullRefresh();

                if (mOnRefreshListener != null) {
                    mOnRefreshListener.onRefresh();
                }
            }

            if (mPullStatus == EPullStatus.PULL_DOWN) {
                if (Value.printLog) {
                    NqLog.d(TAG, "mPullStatus = EPullStatus.PULL_DOWN");
                }

                setPullStatus(EPullStatus.PULL_NORMAL);

                resetHeader();
            }
        }

        /**
         * 是否需要滚动
         * 
         * @param deltaY
         * @return
         */
        boolean needScroll(int deltaY) {
            return (deltaY > mTouchSlop);
        }

        private int performActionMove(MotionEvent event) {
            if (mListView == null) {
                return FALSE;
            }
            mFirstVisibleItem = mListView.getFirstVisiblePosition();

            // // 如果mFirstVisibleItem的下标值超出mListview的范围，则退出，让系统做处理
            // if (mFirstVisibleItem < 0 || mFirstVisibleItem >= mListView.getChildCount())
            // return FALSE;

            // touchdown then move,reset selected state
            int position = mListView.pointToPosition((int) event.getX(), (int) event.getY());
            if (needScroll((int) Math.abs(event.getRawY() - mFirstMotionY))) {
                if (mOnTouchDownUpListener != null && position != ListView.INVALID_POSITION) {
                    mOnTouchDownUpListener.OnTouchUp(position);
                    if (Value.printLog) {
                        NqLog.d(TAG, "moving to position: " + position);
                    }
                }
            }

            int yPos = getScrollY();
            // // 设置当前状态
            // if (yPos >= 0 && mFirstVisibleItem == 0 &&
            // mListView.getChildAt(mFirstVisibleItem).getTop() == 0) {
            // setPullStatus(EPullStatus.PULL_DOWN);
            // }

            // 部分手机不会接收到MotionEvent.ACTION_DOWN消息
            if (mPreMotionY == -1) {
                mPreMotionY = event.getRawY();
                mFirstMotionY = mPreMotionY;
                mPointPosition = position;
                return TRUE;
            }

            // 如果listview的滚动条处于最顶端，用户向上滑动屏幕
            // if (mPullStatus == EPullStatus.PULL_DOWN) {
            // if (mPreMotionY > event.getRawY()
            // && mFirstVisibleItem == 0 &&
            // mListView.getChildAt(mFirstVisibleItem).getTop() == 0 &&
            // yPos >= 0
            // && mPullStatus == EPullStatus.PULL_NORMAL) {
            // // scrollTo(0, 0);
            // if(Value.printLog){NqLog.d(TAG, "step:1");}
            // setPullStatus(EPullStatus.PULL_NORMAL);
            // return FALSE;
            // }
            // }
            // NqLog.d(TAG, "mPreMotionY:" + mPreMotionY + " event.getRawY():" +
            // event.getRawY());

            int paddingTop = mListView.getPaddingTop();

            if (EPullStatus.PULL_NORMAL == mPullStatus) {
                if (mPreMotionY >= event.getRawY()
                        || mFirstVisibleItem != 0
                        || (mListView.getChildAt(0) != null && mListView.getChildAt(0).getTop() != /* 0 */paddingTop)) {
                    // if(Value.printLog){NqLog.d(TAG, "step:1");}
                    return FALSE;
                }
            }

            // 如果listview处于下拉状态时
            if (mPullStatus == EPullStatus.PULL_DOWN || mPullStatus == EPullStatus.PULL_RELEASE || mPullStatus == EPullStatus.PULL_NORMAL) {
                if (Math.abs(event.getRawY() - mFirstMotionY) > mTouchSlop) {
                    mMoved = true;
                    if (mPointPosition != ListView.INVALID_POSITION) {
                        View view = mListView.getChildAt(mPointPosition - mFirstVisibleItem);
                        mListView.setPressed(false);
                        if (view != null) {
                            view.setPressed(false);
                        }
                        // view.setBackgroundColor(Color.TRANSPARENT);

                        mListView.requestDisallowInterceptTouchEvent(true);
                    }
                }
                // if(Value.printLog){NqLog.d(TAG, "step:2");}

                int listViewHeight = mListView.getHeight();
                double division = ((double) (listViewHeight)) / (listViewHeight - 2 * (-yPos)) * 1.2;

                // 避免division大于5或者小于0，原因如下：
                // division大于5会使得listview移动距离非常小，所以避免division大于5
                // division小于0是一种异常，由listview向下移动的距离过大引起的
                if (division > 5 || division <= 0) {
                    division = 5;
                }

                // 避免division小于1大于0，原因如下：
                // division小于1大于0，会使得listview向下移动距离大于手势移动距离
                if (division <= 1) {
                    division = 1;
                }
                if (Value.printLog) {
                    NqLog.d("division:" + division);
                }

                int y = (int) ((event.getRawY() - mPreMotionY) / division);
                if (-yPos + y < 0) {
                    y = yPos;
                }

                // 保证listview向下移动的距离要小于mHeaderView的高度或者小于listview的高度
                int tmp = mHeaderView.getHeight() > listViewHeight ? mHeaderView.getHeight() : listViewHeight;
                if (-yPos + y > tmp) {
                    y = tmp + yPos;
                }

                scrollBy(0, -y);
                mPreMotionY = event.getRawY();

                // 设置新的状态
                if (-getScrollY() >= mHeaderView.getHeight()) {
                    setPullStatus(EPullStatus.PULL_RELEASE);
                } else {
                    setPullStatus(EPullStatus.PULL_DOWN);
                }

                return TRUE;
            }

            return OTHER;
        }

    };

    private final AdapterView.OnItemClickListener ON_ITEM_CLICK_LISTENER = new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (mMoved == true) {
                mMoved = false;
                return;
            }

            // remove footerview added outside
            final int FOOTER_VIEW_COUNT_INSIDE = 1;
            final int obtainMorePosition = mListView.getCount() - mListView.getFooterViewsCount() + FOOTER_VIEW_COUNT_INSIDE - 1;

            if (arg2 == obtainMorePosition && mListView.getFooterViewsCount() > 0 && mListView.getCount() > 0) {
                if (mOnObtainMoreListener != null && mPullStatus == EPullStatus.PULL_NORMAL) {
                    setPullStatus(EPullStatus.MORE_REFRESHING);
                    mOnObtainMoreListener.onObtainMore(arg1);
                }
            } else {
                if (mPullStatus == EPullStatus.PULL_DOWN || mPullStatus == EPullStatus.PULL_RELEASE) {
                    return;
                }
                if (mPullDownViewOnItemClickListener != null) {
                    mPullDownViewOnItemClickListener.onItemClick(arg0, arg1, arg2, arg3);
                }
            }
        }
    };

    private final OnHeaderViewStateChangeListener ON_HEADER_VIEW_STATE_CHANGE_LISTENER = new OnHeaderViewStateChange();

    private class OnHeaderViewStateChange implements OnHeaderViewStateChangeListener {
        // 动画效果
        private RotateAnimation mFlipAnimation; // 变为向下的箭头
        private RotateAnimation mReverseFlipAnimation; // 变为逆向的箭头

        public OnHeaderViewStateChange() {
            // 初始化动画
            mFlipAnimation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            mFlipAnimation.setInterpolator(new LinearInterpolator());
            mFlipAnimation.setDuration(250);
            mFlipAnimation.setFillAfter(true);

            mReverseFlipAnimation = new RotateAnimation(
                    -180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            mReverseFlipAnimation.setInterpolator(new LinearInterpolator());
            mReverseFlipAnimation.setDuration(250);
            mReverseFlipAnimation.setFillAfter(true);
        }

        @Override
        public void onStateChange(View headView, View footerView, EPullStatus status) {
            TextView textView = (TextView) headView.findViewById(R.id.status_info);
            ProgressBar progressBar = (ProgressBar) headView.findViewById(R.id.refresh_progress);
            ImageView imageView = (ImageView) headView.findViewById(R.id.status_img);

            if (status == PullDownView.EPullStatus.PULL_DOWN) {
                textView.setText(mStrHeaderStatusInfo[0]);
                progressBar.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
                // imageView.setImageDrawable(getContext().getResources().getDrawable(mArrowIcon));

                imageView.setImageResource(mArrowIcon);
                imageView.clearAnimation();
                imageView.startAnimation(mReverseFlipAnimation);
            } else if (status == PullDownView.EPullStatus.PULL_RELEASE) {
                textView.setText(mStrHeaderStatusInfo[1]);

                imageView.setImageResource(mArrowIcon);
                imageView.clearAnimation();
                imageView.startAnimation(mFlipAnimation);

            } else if (status == PullDownView.EPullStatus.PULL_REFRESHING) {
                textView.setText(mStrHeaderStatusInfo[2]);

                imageView.setVisibility(View.GONE);
                imageView.setImageDrawable(null);
                progressBar.setVisibility(View.VISIBLE);

                // 关闭更多按钮的事件响应
                if (null != footerView) {
                    footerView.setClickable(false);
                }
            } else if (status == PullDownView.EPullStatus.MORE_REFRESHING) {
                ((ProgressBar) footerView.findViewById(R.id.refresh_progress)).setVisibility(View.VISIBLE);
                ((TextView)footerView.findViewById(R.id.textView_obtain_more)).setText(R.string.is_obtaining_more);
            }
        }

    };

    public interface OnRefreshListener {

        public void onRefresh();
    }

    public interface OnHeaderViewStateChangeListener {

        public void onStateChange(View headView, View footerView, EPullStatus status);
    }

    public interface OnObtainMoreListener {

        public void onObtainMore(View footerView);
    }

    public interface OnItemClickListener {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    /**
     * 用于监听touch事件的开始和结束 Class description
     * 
     * @author ramonqlee
     * @since 2012-2-3
     */
    public interface OnTouchDownUpListener {
        public void onTouchDown(int position);

        public void OnTouchUp(int position);
    }

    /**
     * 监听开始下拉的listener
     * 
     * @author SunHang
     * @since 2012-2-7
     */
    public interface OnPullDownListener {
        public void startPullDown();
    }

    /**
     * 
     * 当HeaderView回弹到初始位置时触发
     * 
     * @author ZhangChunliang
     * @since 2012-2-14
     */
    public interface OnPullDownBackListener {
        public void onPullDownBack();
    }

}
